package jh.poj.builder;

import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;

public class TXTDocumentBuilder implements DocumentBuilder {

	private String author = null;
	private String title = null;
	private final StringBuilder text;
	private final static int MAX_CHAPTER_LEVEL = 4;
	private int currentChepterLevel = 0;
	private final int[] numeringChapters;
	
	private final ChaptersNumering mChaptersNumering;
		
	public TXTDocumentBuilder(ChaptersNumering mChaptersNumering) {
		this.mChaptersNumering=mChaptersNumering;
		numeringChapters = new int[MAX_CHAPTER_LEVEL];
		text = new StringBuilder();
	}

	@Override
	public DocumentBuilder addTitle(String title) {
		if (title == null)
			throw new IllegalArgumentException("jh title is null");
		if (title.isEmpty())
			throw new IllegalArgumentException("jh title is empty!");
		this.title = title;
		return this;

	}

	@Override
	public DocumentBuilder addAuthor(String author) {
		if (author == null)
			throw new IllegalArgumentException("jh author is null");
		if (author.isEmpty())
			throw new IllegalArgumentException("jh author is empty!");
		this.author = author;
		return this;
	}

	@Override
	public DocumentBuilder addChapter(String chapterName, int level) {
		if (chapterName == null)
			throw new IllegalArgumentException("jh chapterName is null");
		if (level < 0)
			throw new IllegalArgumentException(
					"jh chapter level to low! must be bettwen 0 and 4 ");
		if (level > MAX_CHAPTER_LEVEL - 1)
			throw new IllegalArgumentException(
					"jh chapter level to high! must be bettwen 0 and 4 ");

		if (level < currentChepterLevel) {
			for (int i = MAX_CHAPTER_LEVEL - 1; i > level; i--)
				numeringChapters[i] = 0;
		}
		currentChepterLevel = level;
		numeringChapters[currentChepterLevel]++;

		text.append(getCurrentTabs());
		if (mChaptersNumering==ChaptersNumering.NORMAL) {
			text.append(Integer.toString(numeringChapters[0]));
			for (int i = 1; i <= currentChepterLevel; ++i) {
				
				if(numeringChapters[i]==0)
					text.append("." + Integer.toString(numeringChapters[i]+1));
				else
					text.append("." + Integer.toString(numeringChapters[i]));
			}
			text.append(" -- ");
		}

		text.append(chapterName + "\n");
		return this;
	}
	@Override
	public DocumentBuilder addParagraph(String paragraph) {
		if (text == null)
			throw new IllegalArgumentException("jh text is null");
		if (paragraph.isEmpty())
			throw new IllegalArgumentException("jh paragraph is empty!");
		StringTokenizer tok = new StringTokenizer(paragraph, "\n");

		text.append(getCurrentTabs()).append("\t").append(tok.nextToken())
				.append("\n");
		while (tok.hasMoreTokens()) {
			text.append(getCurrentTabs()).append(tok.nextToken() + "\n");
		}

		return this;
	}

	private String getCurrentTabs() {
		StringBuilder tabs = new StringBuilder();
		for (int i = 0; i < currentChepterLevel; ++i) {
			tabs.append("\t");
		}
		return tabs.toString();
	}

	String create() {
		StringBuilder product = new StringBuilder();
		if (title != null)
			product.append("\t\t\t").append(title).append("\n\n");

		product.append(text);

		if (author != null)
			product.append("\t\t\t\t\t\t").append(author);

		return product.toString();
	}

	void saveToFile(String path) {
		if (path == null)
			throw new IllegalArgumentException("path is null");
		if (path.isEmpty())
			throw new IllegalArgumentException("path is empty");

		FileWriter file;
		try {
			file = new FileWriter(path + ".txt");
			file.write(create());
			file.close();
		} catch (IOException e) {
			System.out.println("problem with file:" + e.getMessage());
		}

	}

	@Override
	public DocumentBuilder addBulleted(String bullet) {
		text.append(getCurrentTabs() + "*" + bullet + "\n");
		return this;
	}
}
